﻿using Dynamic.Json;
using static System.Net.WebRequestMethods;

namespace Middleware
{
    public class CheckMiddleware
    {
        private readonly RequestDelegate  _next;

        public CheckMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            string password = context.Request.Query["password"];
            if (password == "123")
            {
                if (context.Request.HasJsonContentType())
                {
                    var stream = context.Request.BodyReader.AsStream();
                    dynamic? obj = await DJson.ParseAsync(stream);
                    context.Items["BodyJson"] = obj;
                }
                await _next.Invoke(context);
            }
            else
            {
                context.Response.StatusCode = 401;
            }
        }
    }
}
